package smartlifev30.com.sdk.api.gateway_app;

import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

import java.util.ArrayList;
import java.util.List;


import smartlifev30.com.sdk.util.JsonUtil;
import smartlifev30.com.sdk.api.CallBack;
import smartlifev30.com.sdk.api.ConstantGateway;
import smartlifev30.com.sdk.bean.request.AddTimerRequest;
import smartlifev30.com.sdk.bean.request.DelTimerRequest;
import smartlifev30.com.sdk.bean.request.EditTimerRequest;
import smartlifev30.com.sdk.bean.request.QueryTimerRequest;
import smartlifev30.com.sdk.bean.response.LoginResponse;
import smartlifev30.com.sdk.bean.response.TimeQueryResponse;
import smartlifev30.com.sdk.net.baiwei.NetManager;
import smartlifev30.com.sdk.net.baiwei.ResponseHelper;

/**
 * Created by tys on 2018/8/13.
 */

public class TimerMgmtGateway implements ITimerMgmtGateway {

    @Override
    public void queryTimer(@NonNull String userName, @NonNull String token, @NonNull String sn,
                           @Nullable List<Integer> timerId, @NonNull CallBack<TimeQueryResponse> callBack) {
        QueryTimerRequest queryTimerRequest = new QueryTimerRequest();
        queryTimerRequest.setFrom(userName);
        queryTimerRequest.setTo(sn);
        queryTimerRequest.setMsg_class(ConstantGateway.MSG_CLASS_TIMER);
        queryTimerRequest.setMsg_name("timer_query");
        queryTimerRequest.setMsg_type(ConstantGateway.MSG_TYPE_GET);
        queryTimerRequest.setToken(token);
        List<QueryTimerRequest.TimerList> list = null;
        if (timerId != null) {
            list = new ArrayList<>();
            for (Integer id : timerId) {
                list.add(queryTimerRequest.new TimerList(id));
            }
        }
        queryTimerRequest.setTimer_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(queryTimerRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void addTimer(@NonNull String userName, @NonNull String token, @NonNull String sn,
                         @NonNull AddTimerRequest.Timer timer, @NonNull CallBack<LoginResponse> callBack) {
        AddTimerRequest addTimerRequest = new AddTimerRequest();
        addTimerRequest.setFrom(userName);
        addTimerRequest.setTo(sn);
        addTimerRequest.setMsg_id(callBack.getMsgId());
        addTimerRequest.setMsg_class(ConstantGateway.MSG_CLASS_TIMER);
        addTimerRequest.setMsg_name("timer_add");
        addTimerRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        addTimerRequest.setToken(token);
        addTimerRequest.setTimer(timer);
        NetManager.getInstance().sendMessage(JsonUtil.toString(addTimerRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void delTimer(@NonNull String userName, @NonNull String token, @NonNull String sn,
                         @Nullable List<Integer> timerId, @NonNull CallBack<LoginResponse> callBack) {
        DelTimerRequest delTimerRequest = new DelTimerRequest();
        delTimerRequest.setFrom(userName);
        delTimerRequest.setTo(sn);
        delTimerRequest.setMsg_id(callBack.getMsgId());
        delTimerRequest.setMsg_class(ConstantGateway.MSG_CLASS_TIMER);
        delTimerRequest.setMsg_name("timer_del");
        delTimerRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        delTimerRequest.setToken(token);
        List<DelTimerRequest.TimeList> list = null;
        if (timerId != null) {
            list = new ArrayList<>();
            for (Integer id : timerId) {
                list.add(delTimerRequest.new TimeList(id));
            }
        }
        delTimerRequest.setTimer_list(list);
        NetManager.getInstance().sendMessage(JsonUtil.toString(delTimerRequest));
        ResponseHelper.addListen(callBack);
    }

    @Override
    public void editTimer(@NonNull String userName, @NonNull String token, @NonNull String sn, @NonNull EditTimerRequest.Timer timer, @NonNull CallBack<LoginResponse> callBack) {
        EditTimerRequest editTimerRequest = new EditTimerRequest();
        editTimerRequest.setFrom(userName);
        editTimerRequest.setTo(sn);
        editTimerRequest.setMsg_id(callBack.getMsgId());
        editTimerRequest.setMsg_class(ConstantGateway.MSG_CLASS_TIMER);
        editTimerRequest.setMsg_name("timer_add");
        editTimerRequest.setMsg_type(ConstantGateway.MSG_TYPE_SET);
        editTimerRequest.setToken(token);
        editTimerRequest.setTimer(timer);
        NetManager.getInstance().sendMessage(JsonUtil.toString(editTimerRequest));
        ResponseHelper.addListen(callBack);
    }

}
